//
//  RegisterViewController.m
//  02-HomeWork
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 danming. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *backGroundImage;
@property (nonatomic, strong) UIImageView *logoImage;
@property (nonatomic, strong) UITextField *phoneNumberTextField;
@property (nonatomic, strong) UITextField *securityCodeTextField;
@property (nonatomic, strong) UIButton *phoneNumberButton;
@property (nonatomic, strong) UIButton *securityCodeButton;
@property (nonatomic, strong) UIButton *getCodeButton;
@property (nonatomic, strong) UIButton *delegateButton;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *returnButton;
@property (nonatomic, strong) UIButton *leaveEditButton;
@property (nonatomic, strong) UIImageView *registerImage;
@property (nonatomic, strong) UILabel *labelOfHint;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createbackGroundImage];
    [self createTextField];
    [self createLabel];
    [self createButton];
    [self configNavigationBar];
}
#pragma mark - 设置bar的属性
- (void)configNavigationBar {
    self.navigationItem.title = @"注册";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor colorWithRed:0.169 green:0.788 blue:0.482 alpha:1.000]}];
}
#pragma mark - 创建背景图片和logo图片
- (void)createbackGroundImage {
    /*********** 创建背景图片 ***********/
    self.backGroundImage = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _backGroundImage.image = [UIImage imageNamed:@"welcome_2"];
    _backGroundImage.alpha = 0.9;
    [self.view addSubview:_backGroundImage];
    /************ 创建logo *************/
    self.logoImage = [[UIImageView alloc] initWithFrame:CGRectMake(157, 120, 100, 160)];
    _logoImage.image = [UIImage imageNamed:@"logo_lagou"];
    [_backGroundImage addSubview:_logoImage];
}
#pragma mark - 创建textField
- (void)createTextField {
    /********** 创建两个textField下面的UIView ************/
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(15, 320, 384, 101)];
    [self.view addSubview:backGroundView];
    /************ 创建两个TextField ************/
    self.phoneNumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(55, 0, 324, 50)];
    self.securityCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(55, 51, 324, 50)];
    _phoneNumberTextField.placeholder = @" 请输入手机号";
    _securityCodeTextField.placeholder = @" 请输入验证码";
    _phoneNumberTextField.tag = 1;
    _securityCodeTextField.tag = 2;
    _phoneNumberTextField.backgroundColor = [UIColor blackColor];
    _securityCodeTextField.backgroundColor = [UIColor blackColor];
    _phoneNumberTextField.alpha = 0.4f;
    _securityCodeTextField.alpha = 0.4f;
    _phoneNumberTextField.delegate = self;
    _securityCodeTextField.delegate = self;
    _securityCodeTextField.textColor = [UIColor redColor];
    _phoneNumberTextField.textColor = [UIColor redColor];
    // 设置占位符的字体颜色和大小
    [_phoneNumberTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_phoneNumberTextField setValue:[UIFont boldSystemFontOfSize:17] forKeyPath:@"_placeholderLabel.font"];
    [_securityCodeTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_securityCodeTextField setValue:[UIFont boldSystemFontOfSize:17] forKeyPath:@"_placeholderLabel.font"];
    [backGroundView addSubview:_phoneNumberTextField];
    [backGroundView addSubview:_securityCodeTextField];
    /********** 创建两个小button ********/
    self.phoneNumberButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.securityCodeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _phoneNumberButton.frame = CGRectMake(5, 0, 50, 50);
    _securityCodeButton.frame = CGRectMake(5, 51, 50, 50);
    // 给图片加渲染, 显示图片原图效果
    [_phoneNumberButton setImage:[[UIImage imageNamed:@"Unknown"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_securityCodeButton setImage:[[UIImage imageNamed:@"Unknown-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    _phoneNumberButton.backgroundColor = [UIColor blackColor];
    _securityCodeButton.backgroundColor = [UIColor blackColor];
    [backGroundView addSubview:_phoneNumberButton];
    [backGroundView addSubview:_securityCodeButton];
    // 用贝塞尔曲线画圆角
    UIBezierPath *phoneNumberBezier = [UIBezierPath bezierPathWithRoundedRect:_phoneNumberTextField.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *phoneNumberLayer = [[CAShapeLayer alloc] init];
    phoneNumberLayer.frame = _phoneNumberTextField.bounds;
    phoneNumberLayer.path = phoneNumberBezier.CGPath;
    _phoneNumberTextField.layer.mask = phoneNumberLayer;
    
    UIBezierPath *securityCodeBezier = [UIBezierPath bezierPathWithRoundedRect:_securityCodeTextField.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *securityCodeLayer = [[CAShapeLayer alloc] init];
    securityCodeLayer.frame = _securityCodeTextField.bounds;
    securityCodeLayer.path = securityCodeBezier.CGPath;
    _securityCodeTextField.layer.mask = securityCodeLayer;
    
    UIBezierPath *phoneNumberButtonBezier = [UIBezierPath bezierPathWithRoundedRect:_phoneNumberButton.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *phoneNumberButtonLayer = [[CAShapeLayer alloc] init];
    phoneNumberButtonLayer.frame = _phoneNumberButton.bounds;
    phoneNumberButtonLayer.path = phoneNumberButtonBezier.CGPath;
    _phoneNumberButton.layer.mask = phoneNumberButtonLayer;
    
    UIBezierPath *securityCodeButtonBezier = [UIBezierPath bezierPathWithRoundedRect:_securityCodeButton.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *securityCodeButtonLayer = [[CAShapeLayer alloc] init];
    securityCodeButtonLayer.frame = _securityCodeButton.bounds;
    securityCodeButtonLayer.path = securityCodeButtonBezier.CGPath;
    _securityCodeButton.layer.mask = securityCodeButtonLayer;

}
#pragma mark - 创建label
- (void)createLabel {
    self.registerImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 440, 25, 20)];
    _registerImage.image = [[UIImage imageNamed:@"Unknown-8"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.view addSubview:_registerImage];
    
    self.labelOfHint = [[UILabel alloc] initWithFrame:CGRectMake(80, 440, 137, 20)];
    _labelOfHint.text = @"注册代表你已同意";
    _labelOfHint.textColor = [UIColor lightGrayColor];
    [self.view addSubview:_labelOfHint];
}
#pragma mark - 创建button
- (void) createButton {
    // 创建拉勾协议button
    self.delegateButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_delegateButton setTitle:@"拉勾用户协议" forState:UIControlStateNormal];
    _delegateButton.titleLabel.font = [UIFont systemFontOfSize:17];
    _delegateButton.frame = CGRectMake(215, 440, 105, 20);
    [_delegateButton setTitleColor:[UIColor colorWithRed:0.00 green:0.72 blue:0.53 alpha:1.00] forState:UIControlStateNormal];
    [self.view addSubview:_delegateButton];
    // 创建注册button
    self.registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_registerButton setTitle:@"注 册" forState:UIControlStateNormal];
    [_registerButton setTitleColor:[UIColor colorWithRed:0.10 green:0.85 blue:0.53 alpha:1.00] forState:UIControlStateNormal];
    // 改变button上的字体大小
    _registerButton.titleLabel.font = [UIFont systemFontOfSize:20];
    _registerButton.backgroundColor = [UIColor colorWithRed:0.00 green:0.68 blue:0.53 alpha:1.00];
    _registerButton.frame = CGRectMake(20, 500, 374, 50);
    _registerButton.layer.cornerRadius = 5;
    [_registerButton addTarget:self action:@selector(clickRegisterButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerButton];
    
    // 创建返回button
    self.returnButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_returnButton setTitle:@"返回登录" forState:UIControlStateNormal];
    [_returnButton setTitleColor:[UIColor colorWithRed:0.10 green:0.78 blue:0.53 alpha:1.00] forState:UIControlStateNormal];
    _returnButton.backgroundColor = [UIColor colorWithRed:0.00 green:0.68 blue:0.53 alpha:0.20];
    _returnButton.titleLabel.font = [UIFont systemFontOfSize:20];
    _returnButton.frame = CGRectMake(20, 580, 374, 50);
    // button的边框宽
    _returnButton.layer.borderWidth = 2;
    // button圆角
    _returnButton.layer.cornerRadius = 5;
    // button边框颜色
    _returnButton.layer.borderColor = [UIColor colorWithRed:0.00 green:0.68 blue:0.53 alpha:1.00].CGColor;
    [_returnButton addTarget:self action:@selector(clickReturnButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_returnButton];
    
}
#pragma mark - 变色
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 1) {
        [_phoneNumberButton setImage:[[UIImage imageNamed:@"Unknown-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    } else {
        [_securityCodeButton setImage:[[UIImage imageNamed:@"Unknown-3"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField.tag == 1) {
        [_phoneNumberButton setImage:[[UIImage imageNamed:@"Unknown"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    } else {
        [_securityCodeButton setImage:[[UIImage imageNamed:@"Unknown-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    return YES;
}
#pragma mark - 隐藏键盘(协议方法)
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - 实现点击方法
- (void)clickRegisterButton:(UIButton *)button {
    // 模态
//    [self dismissViewControllerAnimated:YES completion:nil];
    // pop
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)clickReturnButton:(UIButton *)button {
    // 模态
//    [self dismissViewControllerAnimated:YES completion:nil];
    // pop
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 实现点击空白出键盘回收
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
